﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using Telerik.Windows;
using Telerik.Windows.Controls;

namespace LeoK.Common
{
    /// <summary>
    /// DataSpanSelector.xaml 的交互逻辑
    /// </summary>
    public partial class DataSpanSelector : UserControl
    {
        public Action SelectionChanged;
        public DateTime StartDate { get; set; }

        public DateTime EndDate
        {
            get { return _endDate.AddDays(1).AddMinutes(-1); }
            set { _endDate = value; }
        }

        private DataSpanType _curType = DataSpanType.Month;
        private bool _canMove = true;
        private DateTime _endDate;

        public DataSpanSelector()
        {
            InitializeComponent();

            StartDate = DateTime.Today.AddDays(1 - DateTime.Today.Day);
            _endDate = StartDate.AddMonths(1).AddDays(-1);
            tbDateSpan.Text = "月：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
        }

        private void btnPrev_OnClick(object sender, MouseButtonEventArgs e)
        {
            if (!_canMove) return;

            AddStep(-1);
        }

        private void btnNext_OnClick(object sender, MouseButtonEventArgs e)
        {
            if (!_canMove) return;

            AddStep(1);
        }

        private void AddStep(int rate)
        {
            switch (_curType)
            {
                case DataSpanType.Week:
                    StartDate = StartDate.AddDays(rate*7);
                    _endDate = _endDate.AddDays(rate*7);
                    tbDateSpan.Text = "周：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    break;
                case DataSpanType.Month:
                    StartDate = StartDate.AddMonths(rate);
                    _endDate = StartDate.AddMonths(1).AddDays(-1);
                    tbDateSpan.Text = "月：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    break;
                case DataSpanType.Season:
                    StartDate = StartDate.AddMonths(rate * 3);
                    _endDate = StartDate.AddMonths(rate * 3).AddDays(-1);
                    tbDateSpan.Text = "季：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    break;
                case DataSpanType.Year:
                    StartDate = StartDate.AddYears(rate);
                    _endDate = _endDate.AddYears(rate);
                    tbDateSpan.Text = "年：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    break;
            }

            if (SelectionChanged != null)
            {
                SelectionChanged();
            }
        }

        private void MenuBase_OnItemClick(object sender, RadRoutedEventArgs e)
        {
            RadMenuItem selMenuItem = e.Source as RadMenuItem;

            _canMove = true;
            _curType = (DataSpanType)Enum.Parse(typeof (DataSpanType), selMenuItem.Tag.ToString());
            switch (_curType)
            {
                case DataSpanType.All:
                    StartDate = new DateTime(2015, 1, 1);
                    _endDate = DateTime.Today;
                    tbDateSpan.Text = "所有："+ StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    _canMove = false;
                    break;
                case DataSpanType.Week:
                    StartDate = DateTime.Today.AddDays(1 - Convert.ToInt32(DateTime.Today.DayOfWeek.ToString("d")));
                    _endDate = StartDate.AddDays(6);
                    tbDateSpan.Text = "周：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    break;
                case DataSpanType.Month:
                    StartDate = DateTime.Today.AddDays(1 - DateTime.Today.Day);
                    _endDate = StartDate.AddMonths(1).AddDays(-1);
                    tbDateSpan.Text = "月：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    break;
                case DataSpanType.Season:
                    StartDate = DateTime.Today.AddMonths(0 - (DateTime.Today.Month - 1) % 3).AddDays(1 - DateTime.Today.Day);
                    _endDate = StartDate.AddMonths(3).AddDays(-1);
                    tbDateSpan.Text = "季：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    break;
                case DataSpanType.Year:
                    StartDate = new DateTime(DateTime.Today.Year, 1, 1);
                    _endDate = new DateTime(DateTime.Today.Year, 12, 31);
                    tbDateSpan.Text = "年：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    break;
                case DataSpanType.LastWeek:
                    StartDate = DateTime.Today.AddDays(-7);
                    _endDate = DateTime.Today;
                    tbDateSpan.Text = "最近一周：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    _canMove = false;
                    break;
                case DataSpanType.LastMonth:
                    StartDate = DateTime.Today.AddMonths(-1);
                    _endDate = DateTime.Today;
                    tbDateSpan.Text = "最近1个月：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    _canMove = false;
                    break;
                case DataSpanType.LastSeason:
                    StartDate = DateTime.Today.AddMonths(-3);
                    _endDate = DateTime.Today;
                    tbDateSpan.Text = "最近3个月：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    _canMove = false;
                    break;
                case DataSpanType.LastYear:
                    StartDate = DateTime.Today.AddYears(-1);
                    _endDate = DateTime.Today;
                    tbDateSpan.Text = "最近一月：" + StartDate.ToString("yyyy/MM/dd") + "到" + _endDate.ToString("yyyy/MM/dd");
                    _canMove = false;
                    break;
            }

            if (SelectionChanged != null)
            {
                SelectionChanged();
            }
        }

        public enum DataSpanType
        {
            All,
            Week,
            Month,
            Season,
            Year,
            LastWeek,
            LastMonth,
            LastSeason,
            LastYear
        }
    }
}
